﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FCS.Emulator;
using System.Net;
using System.Net.Sockets;

namespace FCS.Testing
{
    class eRIOTester
    {
        static ERIO _erio;

        static void Main(string[] args)
        {
            Console.Write("Enter IP Addr: ");

            IPAddress ipa = IPAddress.Parse(Console.ReadLine());

            Console.WriteLine("eRIO Started");
            _erio = new ERIO(ipa, 50000, 50001);

            _erio.ListenAsync(ReceivePacket);

            Console.Read();
        }

        static void ReceivePacket(IAsyncResult ar)
        {
            Console.Write("Received: ");
            UdpState client = (UdpState)ar.AsyncState;

            UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
            IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;

            Byte[] receiveBytes = u.EndReceive(ar, ref e);

            for (int i = 0; i < receiveBytes.Length; i++)
                Console.Write(receiveBytes[i] + " ");
            Console.WriteLine();

            FountainState fs = _erio.ParseToFountainState(receiveBytes);
            _erio.SetState(fs);

            Console.WriteLine("State: " + fs.ToString());

            _erio.SendStatus();
            _erio.ListenAsync(ReceivePacket);            

            //Console.Read();
        }
    }
}
